//
//  NSString+ZCHelp.m
//  UniversalApp
//
//  Created by 凯朱 on 2019/6/1.
//  Copyright © 2019 徐阳. All rights reserved.
//

#import "NSString+ZCHelp.h"

@implementation NSString (ZCHelp)
/**
 日期转成日期字符串 代替方法 +(NSString *)dateStringFromDate:(NSDate *)date formatStr:(NSString *)fomatterStr
 
 @param date 日期
 @param formatStr 格式
 @return 日期字符串
 */
+(NSString *)dateStrFromDate:(NSDate *)date formatStr:(NSString *)formatStr
{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:formatStr];
    NSString *dateStr =  [format stringFromDate:date];
    return dateStr;
}

/**
 计算富文本高度
 
 @param width 宽度
 @param font 字体大小
 @param lineSpace 行距
 @return 文本高度
 */
-(CGFloat)countHeightWithWidth:(CGFloat)width font:(UIFont *)font lineSpace:(CGFloat)lineSpace {
   
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self];
    text.font = font;
    text.lineSpacing = lineSpace;
    
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = lineSpace;
    text.paragraphStyle = paragraphStyle;
    
    CGSize maxSize = CGSizeMake(width, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:text];
    CGFloat reHeight = layout.textBoundingRect.size.height;
    CGFloat siHeight = layout.textBoundingSize.height;
    //这里可能存在四舍五入时候去掉尾数的情况
    if (siHeight - reHeight < 0) {
        return siHeight + 1;
    }
    return siHeight;
}

/**
 任何对象转字符串 代替以下方法

 @param obj 对象
 @return 字符串
 */
+(NSString *)valueToString:(id)obj{
    
    if ([obj isEqual:nil]) {
        return [NSString string];
    }
    
    if ([obj isEqual:[NSNull null]]) {
        return [NSString string];
    }
    
    if ([obj isKindOfClass:[NSString class]]) {
        return obj;
    }
    
    if ([obj isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@", obj];
    }
    
    return [NSString string];
}

-(NSAttributedString *)htmlToAttributeWith:(NSDictionary *)options{
    NSData *data = [self dataUsingEncoding:NSUnicodeStringEncoding];
    NSMutableDictionary *newOptions = [[NSMutableDictionary alloc] initWithDictionary:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}];
    [newOptions addEntriesFromDictionary:options];
    NSAttributedString *html = [[NSAttributedString alloc]initWithData:data
                                                               options:newOptions
                                                    documentAttributes:nil
                                                                 error:nil];
    return html;
}


+(NSString *)stringNoUTCDateByFormatString:(NSString *)formatString date:(NSDate *)date
{
    static  NSDateFormatter * dateFromatter = nil;
    NSString * strDate = nil;
    if(!dateFromatter)
    {
        dateFromatter = [[NSDateFormatter alloc] init];
        [dateFromatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    }
    
    if (date == nil) return @"";
    
    @synchronized(dateFromatter)
    {
        if(formatString!=nil)
        {
            [dateFromatter setDateFormat:formatString];
        }
        strDate = [dateFromatter stringFromDate:date];
    }
    return strDate;
}

/*
 任何对象转字符串并且为空的时候用replaceStr代替
 */
+(NSString *)replaceWithString:(id)obj replaceStr:(NSString *)replaceStr{
    NSString *valueStr = [self valueToString:obj];
    replaceStr = [self valueToString:replaceStr];
    if (valueStr.length == 0) {
        if (replaceStr.length > 0) {
            valueStr = replaceStr;
        }
    }
    return valueStr;
}

-(UIViewController *)nibVCFromBundle{
    if (self == nil || self.length == 0) {
        return nil;
    }
    Class className = NSClassFromString(self);
    id nibViewController = [[className alloc] initWithNibName:self bundle:[NSBundle mainBundle]];
    return nibViewController;
}

#pragma 正则匹配手机号
-(BOOL)isTelNumber
{
    if (self.length == 0) {
        return NO;
    }
    NSString *pattern = @"^1+[3578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

/**
 是6位数字
 */
-(BOOL)isSixNumber{
   
    NSString *pattern = @"^[0-9]{6}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}



@end
